﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Examination.BusinessLogicLayer;

public partial class Index : System.Web.UI.Page
{
    Users user = new Users();//创建Users对象user
    

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Expires = 0;//设置过期时间 
        //Response.Cache.SetNoStore();//在服务端不缓存该页 
        //Response.AppendHeader("Pragma ", "no-cache ");   //在客户端不缓存 

        if (!Page.IsPostBack)
        {
            Response.Expires = 0;//设置过期时间 
            Response.Cache.SetNoStore();//在服务端不缓存该页 
            Response.AppendHeader("Pragma ", "no-cache ");   //在客户端不缓存 
            Session["User"] = "";
            Session["PWD"] = "";
            Session["userID"] = "";
            Session.RemoveAll();
            Session.Abandon();
        }

    }

    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        //Users user = new Users();//创建Users对象user
        string loginname = txtUsername.Value.ToString();
        string loginpwd = txtPWD.Value.ToString();
        String checkCode = Request["checkCode"];
        string code = txtValidatecode.Value.ToString();
        code.ToUpper().ToString();
    
        if (loginname != "" && loginpwd != "")
        {
            if (checkCode == code.ToUpper().ToString())
            {
                ShowInfo(loginname, loginpwd);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('验证码错误,请重新登录！');</script>");

                //Response.Write("<script lanuage=javascript>alert('验证码错误,请重新登录！');location='javascript:history.go(-1)'</script>");
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('用户名和密码不能为空，请检查！');</script>");

        }
    }



    private void ShowInfo(string loginname, string loginpwd)
    {
        if (user.CheckPassword(loginname))//根据用户名编号查询用户密码
        {
            if (user.UserPwd == loginpwd)//输入密码与用户密码相同
            {
                if (object.Equals(Request.Cookies["UserID"], null))
                {
                    CreateCookie();
                }
                else
                {
                    CreateCookie();
                }
               // Session["User"] = UserName;
                //Session["PWD"] = PassWord;
                Session["userID"] = loginname;//存储用户编号
                Response.Redirect("/web/Main.aspx");//转向管理员操作界面                      
            }
            else//密码错误，给出提示
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('密码错误，请重新输入！');</script>");
            }
        }
        else//用户不存在，给出提示
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('该用户不存在，请重新输入！');</script>");
        }

    }


    protected void btnbtnReset_Click(object sender, ImageClickEventArgs e)
    {
        Session["User"] = "";
        Session["PWD"] = "";
        Session["userID"] = "";
        this.txtUsername.Value = null;
        this.txtPWD.Value = null;
        this.txtValidatecode.Value = null;
    }

    private void CreateCookie()
    {
        HttpCookie cookie = new HttpCookie("UserID");
        cookie.Value = this.txtUsername.Value;
        cookie.Expires = DateTime.MaxValue;
        Response.AppendCookie(cookie);
    }


}